import { NDescriptionsItem } from "naive-ui";
import type { DescriptionItemProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type DescriptionItemFactoryConstructorType = {
  props?: DescriptionItemProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  labelSlot?: BaseComponentType;
};

export class DescriptionItemFactory {
  private props: DescriptionItemProps = {};
  private attrs: HTMLAttributes = {};
  private defaultSlot: BaseComponentType = null;
  private labelSlot: BaseComponentType = null;

  constructor(data?: DescriptionItemFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.labelSlot) this.setLabel(data.labelSlot);
  }

  setProps(props: DescriptionItemProps) {
    this.props = props;
    return this;
  }
  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }
  setLabel(component: BaseComponentType) {
    this.labelSlot = component;
    return this;
  }

  create() {
    return (
      <NDescriptionsItem {...this.attrs} {...this.props}>
        {{
          default: ()=> getSlotsDom(this.defaultSlot),
          label: () => getSlotsDom(this.labelSlot),
        }}
      </NDescriptionsItem>
    );
  }
}
